package zing.downloader.controller;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import zing.downloader.entity.Song;
import zing.downloader.interfaces.IHtmlController;
import zing.downloader.utils.Constant;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlController implements IHtmlController {

    @Override
    public Element getBodyElement(String url) {
        try {
            Document doc = Jsoup.connect(url).timeout(30000).get();
            return doc.select(Constant.HTML_BODY_TAG).first();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Element> getElementsByClassName(Element parentElement, String tagName, String className) {
        List<Element> retVal = new ArrayList<Element>();
        Elements elements = parentElement.select(tagName);
        for (Element element : elements) {
            if (className.equals(element.className())) {
                retVal.add(element);
            }
        }
        return retVal;
    }

    @Override
    public Element getElementByClassName(Element parentElement, String tagName, String className) {
        Elements elements = parentElement.select(tagName);
        for (Element element : elements) {
            if (className.equals(element.className())) {
                return element;
            }
        }
        return null;
    }

    @Override
    public List<Song> getListSongFromArtistListing(Element rootElement) {
        List<Song> retVal = new ArrayList<Song>();
        List<Element> elements = rootElement.getElementsByTag(Constant.HTML_LI_TAG);
        for (Element element : elements) {
            Song song = parseSongFromArtistListing(element);
            retVal.add(song);
        }
        return retVal;
    }

    @Override
    public List<Song> getListSongFromPlayerListing(Element rootElement) {
        List<Song> retVal = new ArrayList<Song>();
        List<Element> elements = rootElement.getElementsByTag(Constant.HTML_LI_TAG);
        for (Element element : elements) {
            Song song = parseSongFromPlayerListing(element);
            retVal.add(song);
        }
        return retVal;
    }

    private Song parseSongFromArtistListing(Element rootElement) {
        Song song = new Song();

        int number = Integer.parseInt(getElementByClassName(rootElement, "div span", "song-num").text());
        song.setNumber(number);

        Element songInfo = getElementByClassName(rootElement, "h2 a", "_strCut _trackLink");
        String songId = getSongIdFromHref(songInfo.attr("href"));
        song.setVipId(songId);

        String songName = songInfo.text();
        song.setName(songName);

        return song;
    }

    private Song parseSongFromPlayerListing(Element rootElement) {
        Song song = new Song();
        int number = Integer.parseInt(rootElement.getElementsByTag("span").get(0).text());
        song.setNumber(number);

        Element addButton = getElementByClassName(rootElement, "div a", "add-lyric _addPlsLite");
        String songId = getSongIdFromHref(addButton.attr("href"));
        song.setVipId(songId);

        Element songNameLink = getElementByClassName(rootElement, "a", "_playItem _strCut");
        String songName = songNameLink.text();

        Element singerNameLink = getElementByClassName(rootElement, "a", "_strCut");
        String singerName = singerNameLink.text();
        song.setName(songName + " - " + singerName);

        return song;
    }

    @Override
    public String getSongIdFromHref(String href) {
        Pattern pattern = Pattern.compile("/([^/\\.]+)\\.html");
        Matcher matcher = pattern.matcher(href);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }


    @Override
    public List<Song> getListSongFromHotMusic(Element rootElement) {
        List<Song> retVal = new ArrayList<Song>();
        List<Element> elements = getElementsByClassName(rootElement, "div", "album-item");
        for (Element element : elements) {
            Song song = parseSongFromHotMusic(element);
            retVal.add(song);
        }
        return retVal;
    }

    private Song parseSongFromHotMusic(Element rootElement) {
        Song song = new Song();
        int number = Integer.parseInt(rootElement.getElementsByTag("span").get(0).text());
        song.setNumber(number);

        Element songInfo = getElementByClassName(rootElement, "div h3 a", "_trackLink");
        String songId = getSongIdFromHref(songInfo.attr("href"));
        song.setVipId(songId);

        String songName = songInfo.text();
        song.setName(songName);

        return song;
    }

}
